<?php
require_once("xforms/Core.php");

class XForms_input extends XForms_Core {
  protected $tpl = '<div class="xforms_input {renderClass}">{renderHelp}{renderLabel}{required}<div><input {renderId} {renderReadOnly} {renderValue}/></div><div class="xforms_validationmessage"></div>{renderHint}</div>';
  protected $script = '.append(new XForms_input({renderJson}))';
  function renderClass(){
    $cssClass="";
    $binding = $this->getBinding();
    if($binding && $binding->getAttribute("relevant")) {
      $xBinding = $this->getXElement($binding);
      switch($xBinding->evalAttribute("relevant")){
        case true: break;
        case false: $cssClass= $cssClass . " xforms_nonvisible";break;
        default:
      }
    }
    return $cssClass;
  }
  function required(){
    $required="";
    $binding = $this->getBinding();
    if($binding && $binding->getAttribute("required")) {
     $xBinding = $this->getXElement($binding);
      switch($xBinding->evalAttribute("required")) {
        case true: $required='<b style="color:red"> *</b>'; break;
        case false: break;
        default:
      }
    }
    return $required;
  }
  function renderReadOnly(){
    $readonly="";
    $binding = $this->getBinding();
    if($binding) {
      switch($binding->getAttribute("readonly")) {
        case "true()": $readonly=' readonly="readonly" disabled="disabled"'; break;
        case "false()": break;
        default:
      }
    }
    return $readonly;
  }
  function renderLabel(){
    return $this->renderChildNodes(array("xforms:label"));
  }
  function renderHelp(){
    return $this->renderChildNodes(array("xforms:help"));
  }
  function renderHint(){
    return $this->renderChildNodes(array("xforms:hint"));
  }
  function renderValue(){
    return ' value="'.htmlentities($this->getValue(), ENT_QUOTES, 'utf-8').'"';
  }
}
